package com.jiazhong.mybatis.m3;

import com.jiazhong.commons.bean.*;
import com.jiazhong.mybatis.m3.mapper.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.Set;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class App {
    private static void a1() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.findEmpById1(7902);
        System.out.println(emp);
        sqlSession.close();
    }
    private static void a2() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.findEmpById2(7902);
        System.out.println(emp);
        sqlSession.close();
    }

    private static void b(){
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ManyToOneEmpMapper mapper = sqlSession.getMapper(ManyToOneEmpMapper.class);
        ManyToOneEmp emp = mapper.findById(7902);
        System.out.println(emp);
        sqlSession.close();
    }
    private static void c(){
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ManyToOneEmpMapper mapper = sqlSession.getMapper(ManyToOneEmpMapper.class);
        ManyToOneDeptMapper deptMapper = sqlSession.getMapper(ManyToOneDeptMapper.class);
        // 只能获取到emp
        ManyToOneEmp emp = mapper.findById2(7902);
        // 获取到部门信息
        ManyToOneDept dept = deptMapper.findById(emp.getDeptNo());
        emp.setDept(dept);
        System.out.println(emp);
        sqlSession.close();
    }


    private static void d(){
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OneToManyDeptMapper mapper = sqlSession.getMapper(OneToManyDeptMapper.class);
        OneToManyDept dept = mapper.findById1(10);
        System.out.println(dept);
        sqlSession.close();
    }

    private static void e(){
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OneToManyDeptMapper mapper = sqlSession.getMapper(OneToManyDeptMapper.class);
        OneToManyDept dept = mapper.findById2(10);
        OneToManyEmpMapper empMapper = sqlSession.getMapper(OneToManyEmpMapper.class);
        Set<OneToManyEmp> emps = empMapper.findById(dept.getDeptNo());
        dept.setEmps(emps);
        System.out.println(dept);
        sqlSession.close();
    }
    public static void main(String[] args) {
       e();
    }
}